
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

dojo.provide("nanosn.widgets.crud.CRUD");
dojo.require("dijit._Widget");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("nanosn.widgets.crud._Grid");
dojo.require("nanosn.widgets.crud._Editor");
dojo.require("nanosn.widgets.crud._ControlBar");
dojo.require("dijit.layout.BorderContainer");


dojo.declare("nanosn.widgets.crud.CRUD", dijit._Widget,
{
    
    _grid: null,
    _editor: null,
    _structure: null,
    _store: null,
    url: null,
    item: null,
    isDirty: false,/* True if data has changed before saving*/

    setIsDirty: function (isDirty /*boolean*/)
    {
        //TODO: trigger event
        if(this.isDirty != isDirty)
            this.isDirty = isDirty;
    },
    

    postMixInProperties: function()
    {

        console.log("postMixInProperties CRUD");

        //The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
        var xhrArgs = {
            url: new String(this.url + "/structure"),
            sync: true,
            handleAs: "json",
            error: function(error){
                targetNode.innerHTML = "An unexpected error occurred: " + error;
            }
        }

        //Call the asynchronous xhrGet
        var deferred = dojo.xhrGet(xhrArgs);
        deferred.addCallback(dojo.hitch(this,"setStructure"));

    //        this.inherited(arguments);
    },

    setStructure: function(data)
    {
        this._structure = data;
    },

    _tabContainer : null,
    _contentPaneEditor: null,
    _contentPaneDataGrid: null,
    borderContainer: null,
    _controlBar: null,

    buildRendering: function()
    {


        console.log("buildRendering CRUD");

        /*
         * _store
         */
        this._store = new dojox.data.JsonRestStore({
            target: this.url
        });

        /*
         * _grid
         */
        this._grid = new dojox.grid.DataGrid({
            structure: this._structure,
            store: this._store
        });

        dojo.connect(this._grid, "onRowClick", this, this.grid_onRowClick);
        dojo.connect(this._grid, "onRowDblClick", this, this.grid_onRowDblClick);

        /*
         * _editor
         */
        this._editor = new nanosn.widgets.crud._Editor({
            container: this
        });

        /***********
         * this._tabContainer
         **********/
        this._tabContainer = new dijit.layout.TabContainer(
        {
            style: "height: 100%; width: 100%;",
            tabPosition: "left",
            tabStrip: "true"
        }, document.createElement("div"));

        /***********
         * this._contentPaneEditor
         ***********/        
        this._contentPaneEditor = new dijit.layout.ContentPane(
        {
            title: "Editor",
            content: this._editor
        });
        
        /***********
         * this._contentPaneDataGrid
         ***********/
        this._contentPaneDataGrid = new dijit.layout.ContentPane(
        {
            title: "Table",
            content: this._grid
        });
        
        this._controlBar = new nanosn.widgets.crud._ControlBar({
            container: this
        })
        contentPaneControlBar = new dijit.layout.ContentPane(
        {
            title: "Control Bar",
            region:'top',
            content: this._controlBar
        });
        dojo.connect(this._controlBar, "SaveOnClick", this, this.SaveClicked);
        
        contentPaneTabContainer = new dijit.layout.ContentPane(
        {
            title: "tabby",
            region:'center',
            content: this._tabContainer
        });

        this.borderContainer = new dijit.layout.BorderContainer({
            style: "height: 100%; width: 100%;"
        });
        this.borderContainer.addChild(contentPaneControlBar);
        this.borderContainer.addChild(contentPaneTabContainer);
        /*
         * domNode
         */
        this.domNode = this.borderContainer.domNode;
        //Start up stuff
        this._tabContainer.addChild(this._contentPaneEditor);
        this._tabContainer.addChild(this._contentPaneDataGrid);
        
    },
    
    postCreate: function()
    {
        console.log("postCreate CRUD");
        
    },

    grid_onRowDblClick : function(e)
    {

        try
        {
            this._tabContainer.selectChild(this._contentPaneEditor);
        }
        catch (e)
        {
            console.error(e);
        }
    },

    grid_onRowClick : function(e)
    {
        rowIndex = e.rowIndex;

        item = this._grid.getItem(rowIndex);
        this.setItem(item);

    },

    setItem: function (item)
    {
        console.log(item);
        this.item = item;
        this._editor.setItem(item);
        this._controlBar.setItem(item);
    },

    SaveClicked: function (e)
    {
      
    },

    startup: function()
    {
        console.log("startup CRUD");
        this._tabContainer.startup();
        this.borderContainer.startup();
        this._tabContainer.selectChild(this._contentPaneDataGrid);

        dojo.connect(this._tabContainer, "selectChild", this, function(){
            
            console.log(this._tabContainer.selectedChildWidget);
            if (this._tabContainer.selectedChildWidget == this._contentPaneDataGrid)
            {
                if(this._grid.selection.getSelected().length == 0)
                    this._controlBar.setInitialMode();
                else
                    this._controlBar.setCancelEditMode();
            }
            else //Editor Tab selected
            {
                
                if(this._grid.selection.getSelected().length != 0)
                    this._controlBar.setEditMode();
            }
        })
    },

    switchToEditorTab: function()
    {
        
        if (this._tabContainer.selectedChildWidget == this._contentPaneDataGrid)
        {
            this._tabContainer.forward();
        }
    },
    switchToTableTab: function()
    {

        if (this._tabContainer.selectedChildWidget == this._contentPaneEditor)
        {
            this._tabContainer.forward();
        }
    }
});
